package main

import (
	"encoding/json"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"time"
	"strings"
)

func autoHomeListHandle(src string, categoryId int, childId int, start int, end int) {
	var finish = false
	for i := start; i <= end; i++ {
		url := fmt.Sprintf(src, i)
		fmt.Println(url)
		doc, err := createDoc(url)
		if err != nil {
			trace(fmt.Sprintf("%s createDoc failed : %v", url, err))
			continue
		}
		doc.Find("#auto-channel-lazyload-article ul").Each(func(j int, s *goquery.Selection) {
			s.Find("li").Each(func(w int, z *goquery.Selection) {
				href, _ := z.Find("a").Attr("href")
				img, _ := z.Find(".article-pic img").Attr("src")

				article, _ := getArticle(href)
				if article.ArticleId > 0 {
					trace(fmt.Sprintf("汽车之家 finish : %s", href))
					finish = true
				}

				if len(href) > 0 && finish == false {
					art := new(ClcwArticle)
					art.CategoryId = categoryId
					art.ChildId = childId
					art.ArticleFocus = img
					art.ArticleThumb = img
					art.FromUrl = href
					art.FromName = "汽车之家"
					jsonStr, err := json.Marshal(art)
					if err != nil {
						trace(fmt.Sprintf("failed: %v", href))
						trace(fmt.Sprintf("json Marshal failed : %v", err))
					} else {
						trace(fmt.Sprintf("lpush : %v", href))
						lpush(conf.Redis.ArticleQueue, jsonStr)
					}
					time.Sleep(time.Duration(conf.Crawler.Interval) * time.Second)
				}
			})
		})

		if finish == true {
			return
		}
	}
}

func autoHomeDetailHandle(art *ClcwArticle) {

	doc, err := createDoc(art.FromUrl)
	if err != nil {
		trace(fmt.Sprintf("%s createDoc failed : %v", art.FromUrl, err))
		return
	}
	all := doc.Find(".page-item-info").Text()
	if len(all) > 0 {
		fromUrl := strings.Split(art.FromUrl, ".html")
		if len(fromUrl) != 2 {
			return
		}
		url := ""
		if strings.Contains(fromUrl[0], "-") {
			urlArr := strings.Split(fromUrl[0], "-")
			if len(urlArr) != 2 {
				return
			}
			url = urlArr[0] + "-all.html" + fromUrl[1]
		} else {
			url = fromUrl[0] + "-all.html" + fromUrl[1]
		}
		fmt.Println(url)
		doc, err = createDoc(url)
		if err != nil {
			trace(fmt.Sprintf("%s createDoc failed : %v", url, err))
			return
		}
	}

	art.ArticleTitle = doc.Find("#articlewrap h1").Text()
	art.ArticleTitle = strReplace(art.ArticleTitle)

	art.Author = doc.Find(".editor-select-wrap em").Next().Text()
	art.Author = strReplace(art.Author)

	art.CreateTime = int(time.Now().Unix())
	art.ArticleContent, _ = doc.Find("#articleContent").Html()

	if len(art.ArticleTitle) <= 0 || len(art.ArticleContent) <= 0 {
		return
	}

	art2utf8(art, "GB18030")

	art.Status = 2
	art.Type = 2
	art.FromName = "汽车之家"

	affected, err := engine.Table("clcw_article_crawler").Insert(art)
	if err != nil {
		trace(fmt.Sprintf("article insert failed: %v", err))
	} else {
		trace(fmt.Sprintf("article insert %d : %s", affected, art.ArticleTitle))
	}
}
